import {getIpLocation} from "@/api/user";
import DateTimeUtils from "@/utils/DateTimeUtils";

export default {
    namespaced: true,
    //存储数据
    state: {
        userInfo: {},
    },
    getters: {},
    //调用方法
    mutations: {
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo;
        }
    },
    actions: {
        async setUserInfo({commit}, userInfo) {
            try {
                const {data} = await getIpLocation();
                const loginTime = DateTimeUtils.getNow();
                userInfo.ipCity = data.province + ' ' + data.city;
                userInfo.loginTime = loginTime;
            } catch (error) {
                console.error(error);
            } finally {
                // 提交mutation，更新状态数据
                commit('setUserInfo', userInfo);
            }
        }
    }
}
